package vodoo.gui.video;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ToolTipManager;
import javax.swing.event.EventListenerList;

import vodoo.video.StreamStatistics;
import vodoo.video.VideoStream;

public class VideoControlPanel extends VideoPanel implements MouseListener {
	//--------------------------------------------------------- CONSTANTES --//
	//---------------------------------------------------------- ATTRIBUTS --//
	
	private EventListenerList listenersList;
	
	private boolean mouseOver;
	
	//------------------------------------------------------ CONSTRUCTEURS --//
	
	public VideoControlPanel(VideoStream stream) {
		super(stream);
		
		listenersList = new EventListenerList();
		
		ToolTipManager.sharedInstance().setInitialDelay(1200);
		
		addMouseListener(this);
		addMouseMotionListener(new MouseMotionListener() {
			public void mouseDragged(MouseEvent e) {
				
			}
			
			public void mouseMoved(MouseEvent e) {
				StreamStatistics stats = getVideoStream().getStreamStatistics();
				setToolTipText("<html><table>" +
						"<tr><td colspan=\"2\"><b>Stream information</b></td></tr>" +
						"<tr><td>Stream rate</td><td>" + stats.GetStreamRate() + " kB/s</td></tr>" +
						"<tr><td>Images received</td><td>" + stats.GetReceivedImages() + "</td></tr>" +
						"<tr><td>Lost images</td><td>" + stats.GetLostImages() + "</td></tr>" +
						"<tr><td>Lost images rate</td><td>" + stats.GetLostImagesRate() + " %</td></tr>" +
						"</table></html>");
			}
		});
	}
	
	//----------------------------------------------------------- METHODES --//
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Graphics2D g2d = (Graphics2D) g;
		
		// Barre transparente
		if (isBuffering() || mouseOver) {
			g.setColor(new Color(0x40000000, true));
			g.fillRect(0, 0, getWidth() - 1, 20);
		}
		
		// Affiche un message si le flux est mis en buffer
		if (isBuffering()) {
			g.setColor(Color.WHITE);
			g.drawString("Buffering...", 6, 14);
		}
		
		// Boutons play - pause / fullscreen / close
		if (mouseOver || !getVideoStream().isStarted()) {
			g.setColor(Color.WHITE);
			
			if (getVideoStream().isPlaying()) {
				// Pause
				Rectangle pause = new Rectangle(0, 0, 4, 12);
				pause.translate(getWidth() - 75, 4);
				g2d.fill(pause);
				pause.translate(7, 0);
				g2d.fill(pause);
			} else {
				// Play
				Polygon play = new Polygon(
						new int[]{0, 0, 14}, new int[]{0, 14, 7}, 3);
				play.translate(getWidth() - 77, 3);
				g.fillPolygon(play);
			}
			
			// Fullscreen
			g.drawRect(getWidth() - 51, 4, 12, 11);
			g.fillRect(getWidth() - 51, 4, 12, 3);
			
			// Close
			g2d.setRenderingHint(
					RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			
			g2d.setStroke(new BasicStroke(2.5f));
			g.drawLine(getWidth() - 24, 5, getWidth() - 15, 14);
			g.drawLine(getWidth() - 15, 5, getWidth() - 24, 14);
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		
		if (y < 14) {
			if (x >= getWidth() - 80 && x < getWidth() - 60)
				playPauseAction();
			else if (x >= getWidth() - 55 && x < getWidth() - 35)
				fireFullscreenButtonAction(this);
			else if (x >= getWidth() - 30 && x < getWidth() - 10)
				fireCloseButtonAction(this);
		}
	}

	public void mouseEntered(MouseEvent e) {
		mouseOver = true;
		repaint();
	}

	public void mouseExited(MouseEvent e) {
		mouseOver = false;
		repaint();
	}

	public void mousePressed(MouseEvent e) {
		// Sans effet
	}

	public void mouseReleased(MouseEvent e) {
		// Sans effet
	}
	
	public void addVideoControlListener(VideoControlListener listener) {
		listenersList.add(VideoControlListener.class, listener);
	}
	
	public void removeVideoControlListener(VideoControlListener listener) {
		listenersList.remove(VideoControlListener.class, listener);
	}
	
	protected VideoControlListener[] getVideoControlListeners() {
		return listenersList.getListeners(VideoControlListener.class);
	}
	
	protected void fireCloseButtonAction(VideoControlPanel source) {
		Object[] listeners = listenersList.getListenerList();
		
		for (int i = listeners.length - 2; i >= 0; i -= 2)
			if (listeners[i] == VideoControlListener.class)
				((VideoControlListener) listeners[i + 1]).closeButtonAction(source);
	}
	
	protected void fireFullscreenButtonAction(VideoControlPanel source) {
		Object[] listeners = listenersList.getListenerList();
		
		for (int i = listeners.length - 2; i >= 0; i -= 2)
			if (listeners[i] == VideoControlListener.class)
				((VideoControlListener) listeners[i + 1]).fullscreenButtonAction(source);
	}
	
	//--------------------------------------------------- METHODES PRIVEES --//
	
	private void playPauseAction() {
		if (getVideoStream().isPlaying())
			pause();
		else
			play();
	}
}
